package com.jiawa.train.business.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.train.business.domain.Train;
import com.jiawa.train.business.domain.TrainExample;
import com.jiawa.train.business.mapper.TrainMapper;
import com.jiawa.train.business.req.TrainQueryReq;
import com.jiawa.train.business.req.TrainSaveReq;
import com.jiawa.train.business.resp.TrainQueryResp;
import com.jiawa.train.business.service.TrainService;
import com.jiawa.train.common.exception.BusinessException;
import com.jiawa.train.common.exception.BusinessExceptionEnum;
import com.jiawa.train.common.resp.PageResp;
import com.jiawa.train.common.util.SnowUtil;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zzl
 * @version 1.0
 */
@Service
public class TrainServiceImpl implements TrainService {
    private static final Logger LOG = LoggerFactory.getLogger(TrainServiceImpl.class);

    @Resource
    private TrainMapper trainMapper;

    /**
     * 新增一条高铁信息
     * @param req
     */
    @Override
    public void save(TrainSaveReq req) {
        DateTime now = DateTime.now();
        Train train = BeanUtil.copyProperties(req, Train.class);
        if(ObjectUtil.isNull(train.getId())) {
            if(isExistByCode(train.getCode())) {
                throw new BusinessException(BusinessExceptionEnum.TRAIN_IS_EXIST);
            } else {
                train.setId(SnowUtil.getSnowflakeNextId());
                train.setCreateTime(now);
                train.setUpdateTime(now);
                trainMapper.insert(train);
            }
        } else {
            train.setUpdateTime(now);
            trainMapper.updateByPrimaryKeySelective(train);
        }
    }

    /**
     * 分页查询高铁信息
     * @param req
     * @return
     */
    @Override
    public PageResp<TrainQueryResp> queryList(TrainQueryReq req) {
        TrainExample trainExample = new TrainExample();
        trainExample.setOrderByClause("code asc");

        LOG.info("查询页码：{}", req.getPage());
        LOG.info("每页条数：{}", req.getSize());
        PageHelper.startPage(req.getPage(), req.getSize());
        List<Train> trainList = trainMapper.selectByExample(trainExample);

        PageInfo<Train> pageInfo = new PageInfo<>(trainList);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

        List<TrainQueryResp> trainQueryRespList = BeanUtil.copyToList(trainList, TrainQueryResp.class);
        PageResp<TrainQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(trainQueryRespList);
        return pageResp;
    }

    /**
     * 根据id删除高铁信息
     * @param id
     */
    @Override
    public void delete(Long id) {
        trainMapper.deleteByPrimaryKey(id);
    }

    /**
     *
     * @return
     */
//    @Transactional
    @Override
    public List<TrainQueryResp> queryAll() {
        LOG.info("查第一次");
        List<Train> trainList = selectAll();
        LOG.info("再查一次");
        trainList = selectAll();
        LOG.info("结束再查一次");
        return BeanUtil.copyToList(trainList, TrainQueryResp.class);
    }

    /**
     * 根据高铁车次查询高铁信息
     * @return
     */
    @Override
    public List<Train> selectAll() {
        TrainExample trainExample = new TrainExample();
        trainExample.setOrderByClause("code asc");
        return trainMapper.selectByExample(trainExample);
    }

    /**
     * 根据高铁车次判断是否存在
     * @param code
     * @return
     */
    private boolean isExistByCode(String code) {
        TrainExample trainExample = new TrainExample();
        trainExample.createCriteria().andCodeEqualTo(code);
        List<Train> list = trainMapper.selectByExample(trainExample);
        if(CollUtil.isNotEmpty(list)) return true;
        else return false;
    }
}
